﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace TinySkill
{

    [CustomEditor(typeof(SkillObject))]
    public class SkillEditor : Editor
    {

        [MenuItem("技能编辑器/上下文菜单/新建")]
        static void New()
        {
            if (Selection.activeGameObject == null)
            {
                Selection.activeGameObject = GameObject.Find("root");
            }

            if (Selection.activeGameObject != null)
            {
                GameObject newObj = null;
                if (Selection.activeGameObject == GameObject.Find("root"))
                {
                    newObj = new GameObject();
                    newObj.AddComponent<SkillObject>();
                    newObj.transform.parent = Selection.activeGameObject.transform;
                    newObj.transform.localPosition = Vector3.zero;
                    newObj.transform.localRotation = Quaternion.identity;
                    newObj.transform.localScale = Vector3.one;
                }
                else
                {
                    newObj = GameObject.Instantiate(Selection.activeGameObject, Selection.activeGameObject.transform.parent);
                }
                int max_id = 0;
                foreach (var id in Data.Skill)
                {
                    max_id = Mathf.Max(id.Key, max_id);
                }
                newObj.GetComponent<SkillObject>().id = max_id + 1;
                config_skill newSkill = new config_skill();
                newSkill.id = max_id + 1;
                Data.Skill[newSkill.id] = newSkill;
                newObj.name = newSkill.id.ToString() + ":" + newSkill.name;
                newObj.GetComponent<SkillObject>().selected_frame_index = -1;
                SkillEditorWindow.MarkSceneDirty();
            }
        }
        [MenuItem("技能编辑器/上下文菜单/复制")]
        static void Copy()
        {
            if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<SkillObject>() != null)
            {
                SkillObject target = Selection.activeGameObject.GetComponent<SkillObject>();
                GameObject newObj = GameObject.Instantiate(Selection.activeGameObject, Selection.activeGameObject.transform.parent);
                int max_id = 0;
                foreach (var id in Data.Skill)
                {
                    max_id = Mathf.Max(id.Key, max_id);
                }
                newObj.GetComponent<SkillObject>().id = max_id + 1;
                config_skill newSkill = ProtoBuf.Serializer.DeepClone<config_skill>(Data.GetSkill(target.id));
                newSkill.id = max_id + 1;
                Data.Skill[newSkill.id] = newSkill;
                newObj.name = newSkill.id.ToString() + ":" + newSkill.name;

                SkillEditorWindow.MarkSceneDirty();
            }
        }

        [MenuItem("技能编辑器/上下文菜单/删除")]
        static void Del()
        {
            if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<SkillObject>() != null)
            {
                SkillObject target = Selection.activeGameObject.GetComponent<SkillObject>();
                Data.Skill.Remove(target.id);
                GameObject.DestroyImmediate(target.gameObject);

                SkillEditorWindow.MarkSceneDirty();
            }
        }

        [InitializeOnLoadMethod]
        static void StartInitializeOnLoadMethod()
        {
            EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
        }

        static void OnHierarchyGUI(int instanceID, Rect selectionRect)
        {
            if (Event.current != null && selectionRect.Contains(Event.current.mousePosition)
                && Event.current.button == 1 && Event.current.type <= EventType.MouseUp)
            {
                GameObject selectedGameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
                //这里可以判断selectedGameObject的条件
                if (selectedGameObject.GetComponent<SkillObject>() != null)
                {
                    Vector2 mousePosition = Event.current.mousePosition;

                    EditorUtility.DisplayPopupMenu(new Rect(mousePosition.x, mousePosition.y, 0, 0), "技能编辑器/上下文菜单", null);
                    Event.current.Use();
                }
            }
        }


        SkillObject myTarget;
        SkillEditorWindow.CustomTypeDrawer drawer;
        SkillEditorWindow.CustomTypeDrawer od;

        ShapeEditor shapeEditor;

        static Dictionary<string, System.Type> shapeEditorTypes;
        static SkillEditor()
        {
            shapeEditorTypes = new Dictionary<string, System.Type>();
            shapeEditorTypes["box"] = typeof(BoxShapeEditor);
            shapeEditorTypes["sphere"] = typeof(SphereShapeEditor);
        }


        static int snap_size = 10;
        static bool snap;

        void OnEnable()
        {
            ResetSceneView();
            SkillEditorWindow.onActionChange = OnActionChange;
        }

        void OnActionChange()
        {
            Repaint();
        }

        object _last_data;

        public override void OnInspectorGUI()
        {
            myTarget = (SkillObject)target;

            EditorGUI.BeginChangeCheck();

            if (myTarget.skill == null)
            {
                base.OnInspectorGUI();
            }
            else
            {
                GUILayout.BeginHorizontal();
                Color defaultColor = GUI.color;

                //foreach(var it in shapEditors)
                //{

                //}

                //GUI.color = editMode == EEditorMode.Box ? Color.yellow : defaultColor;
                //if (GUILayout.Button("矩形",GUILayout.Width(50)))
                //{
                //    editMode = EEditorMode.Box;
                //    InvalideScene();
                //}
                //GUI.color = editMode == EEditorMode.Circle ? Color.yellow : defaultColor;
                //if (GUILayout.Button("圆形", GUILayout.Width(50)))
                //{
                //    editMode = EEditorMode.Circle;
                //    InvalideScene();
                //}
                GUILayout.EndHorizontal();
                GUILayout.Space(10);

                GUI.color = defaultColor;

                if (drawer == null || drawer.obj != myTarget.skill)
                {
                    drawer = new SkillEditorWindow.CustomTypeDrawer(myTarget.skill);
                }

                config_skill skill = (config_skill)drawer.obj;
                int last_id = skill.id;
                string name = skill.name;

                drawer.OnGUI();

                //更改了id
                if (last_id != skill.id)
                {
                    if (Data.Skill.ContainsKey(skill.id))
                    {
                        skill.id = last_id;
                    }
                    else
                    {
                        Data.Skill.Remove(last_id);
                        Data.Skill.Add(skill.id, skill);
                    }
                    myTarget.id = skill.id;
                    myTarget.name = skill.id.ToString() + ":" + skill.name;
                }

                if (name != skill.name)
                {
                    myTarget.name = skill.id.ToString() + ":" + skill.name;
                }



                GUI.color = defaultColor;

                GUILayout.Space(10);
                GUI.color = Color.green;
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(2));
                GUILayout.Space(10);
                GUI.color = defaultColor;


                if (myTarget != null && myTarget.selected_frame != null)
                {
                    if (od == null || od.obj != myTarget.selected_frame || (myTarget.selected_frame != null && _last_data != myTarget.selected_frame.data))
                    {
                        od = new SkillEditorWindow.CustomTypeDrawer(myTarget.selected_frame);
                        od.hide = false;

                        if (myTarget.selected_frame != null)
                            _last_data = myTarget.selected_frame.data;
                        else
                            _last_data = null;
                    }
                }
                else
                {
                    od = null;
                }

                if (od != null)
                    od.OnGUI();
            }

            if (EditorGUI.EndChangeCheck())
            {
                SkillEditorWindow.MarkSceneDirty();
            }
        }

        void ResetSceneView()
        {
            Tools.current = Tool.None;
            Tools.viewTool = ViewTool.None;
            if (myTarget == null)
                return;

            if (SceneView.lastActiveSceneView == null)
                return;


            myTarget.transform.position = Vector3.zero;
            myTarget.transform.localRotation = Quaternion.identity;
            myTarget.transform.localScale = Vector3.one;

            float size = SceneView.lastActiveSceneView.size;
            SceneView.lastActiveSceneView.pivot = Vector3.zero;
            SceneView.lastActiveSceneView.LookAt(Vector3.zero, Quaternion.LookRotation(Vector3.down), size, true);
        }

        //size 屏幕高度
        void ScaleSceneView(float size)
        {
            ResetSceneView();

            float newSize = size;

            SceneView.lastActiveSceneView.LookAt(Vector3.zero, Quaternion.LookRotation(Vector3.down), newSize, true);
        }


        Rect GetBoundaryRect()
        {
            if (shapeEditor != null)
                return shapeEditor.GetBoundaryRect();

            return Rect.zero;
        }

        void OnSceneGUI()
        {
            GUIStyle style = new GUIStyle();
            style.normal.textColor = Color.green;


            Handles.BeginGUI();
            GUILayout.BeginArea(new Rect(0, 0, 300, 100));
            GUILayout.BeginVertical();

            GUILayout.BeginHorizontal();
            GUILayout.Label("视图:" + SceneView.lastActiveSceneView.camera.orthographicSize * 2, style);
            if (GUILayout.Button("重置"))
            {
                ResetSceneView();
            }

            if (GUILayout.Button("最小化"))
            {
                Rect rc = GetBoundaryRect();
                if (rc.size != Vector2.zero)
                {
                    ScaleSceneView(Mathf.Max(Mathf.Abs(rc.xMin), Mathf.Abs(rc.yMin), Mathf.Abs(rc.xMax), Mathf.Abs(rc.yMax)) * 2);
                }
            }
            if (GUILayout.Button("最大化"))
            {
                ScaleSceneView(128);
            }

            GUILayout.EndHorizontal();

            GUILayout.EndVertical();
            GUILayout.EndArea();
            Handles.EndGUI();


            Handles.color = Color.black;
            Handles.DrawLine(new Vector3(-1000, 0, 0), new Vector3(1000, 0, 0));
            Handles.DrawLine(new Vector3(0, 0, -1000), new Vector3(0, 0, 1000));
            Handles.color = Color.white;
            Handles.DrawWireCube(Vector3.zero, new Vector3(72, 1, 128));



            myTarget = (SkillObject)target;
            if (myTarget == null || myTarget.skill == null)
                return;


            if (myTarget.selected_frame == null || myTarget.selected_frame.data == null)
                return;


            if (shapeEditor == null || shapeEditor.owner != myTarget.last_selected_property)
            {
                if (myTarget.last_selected_property != null)
                {
                    foreach (var it in shapeEditorTypes)
                    {
                        FieldInfo fi = myTarget.last_selected_property.GetType().GetField(it.Key);
                        if (fi != null)
                        {
                            if (shapeEditor == null || shapeEditor.GetType() != it.Value)
                            {
                                shapeEditor = Activator.CreateInstance(it.Value) as ShapeEditor;
                            }

                            shapeEditor.Init(myTarget.gameObject, myTarget.last_selected_property, fi);
                            break;
                        }
                    }
                }

            }

            if (shapeEditor == null)
            {
                Handles.BeginGUI();

                GUILayout.BeginArea(new Rect(0, 100, 500, 500));
                style.normal.textColor = Color.red;
                GUILayout.Label("无可编辑形状属性", style);
                GUILayout.EndArea();

                Handles.EndGUI();
                return;
            }


            EditorGUI.BeginChangeCheck();

            if (shapeEditor != null)
                shapeEditor.Draw(Event.current);

            if (EditorGUI.EndChangeCheck())
            {
                SkillEditorWindow.MarkSceneDirty();
            }


            if (Event.current != null)
            {
                switch (Event.current.type)
                {
                    case EventType.MouseDown:
                        if (shapeEditor != null)
                            shapeEditor.OnMouseDown(Event.current);
                        break;
                    case EventType.MouseDrag:
                        if (shapeEditor != null)
                            shapeEditor.OnMouseDrag(Event.current);
                        break;
                    case EventType.MouseUp:
                        if (shapeEditor != null)
                            shapeEditor.OnMouseUp(Event.current);
                        break;
                }
            }

            //防止误操作
            {
                Event e = Event.current;

                int controlID = GUIUtility.GetControlID(FocusType.Passive);

                if (e.type == EventType.Layout)
                {
                    HandleUtility.AddDefaultControl(controlID);
                }
            }


        }

        //对齐一个像素坐标到网格
        float Snap(float x)
        {
            if (x > 0)
            {
                float left_x = ((int)(x / snap_size)) * snap_size;
                float right_x = ((int)(x / snap_size) + 1) * snap_size;
                return Mathf.Abs(x - left_x) > Mathf.Abs(x - right_x) ? right_x : left_x;
            }
            else
            {
                float right_x = ((int)(x / snap_size)) * snap_size;
                float left_x = ((int)(x / snap_size) - 1) * snap_size;
                return Mathf.Abs(x - left_x) > Mathf.Abs(x - right_x) ? right_x : left_x;
            }

        }
        Vector2 Snap(Vector2 pt)
        {
            Vector2 newPos = new Vector2();
            newPos.x = Snap(pt.x);
            newPos.y = Snap(pt.y);
            return newPos;
        }

    }


    /// <summary>
    /// 形状编辑器基类
    /// </summary>
    abstract class ShapeEditor
    {
        //参数所在的对象
        public object owner;
        //参数的字段
        public FieldInfo field;
        //选择的技能所属的对象
        public GameObject gameObject;

        public virtual void Init(GameObject gameObject, object o, FieldInfo field)
        {
            owner = o;
            this.gameObject = gameObject;
            this.field = field;
        }


        public virtual void OnMouseDown(Event e)
        {

        }

        public virtual void OnMouseDrag(Event e)
        {

        }

        public virtual void OnMouseUp(Event e)
        {

        }

        //绘制
        public virtual void Draw(Event e) { }

        //返回包围框
        public abstract Rect GetBoundaryRect();
    }


    /// <summary>
    /// 盒体编辑器，对应字段box，支持数组
    /// </summary>
    class BoxShapeEditor : ShapeEditor
    {
        bool isArray { get { return field.FieldType.IsArray; } }

        Box data;

        Box[] dataArray;
        Box _selectedBox;

        public BoxShapeEditor()
        {
        }

        public override void Init(GameObject gameObject, object o, FieldInfo field)
        {
            base.Init(gameObject, o, field);
            if (isArray)
            {
                dataArray = (Box[])field.GetValue(o);
                if (dataArray == null)
                {
                    dataArray = new Box[0];
                    field.SetValue(o, dataArray);
                }
            }
            else
            {
                data = (Box)field.GetValue(o);
                if (data == null)
                {
                    data = new Box();
                    data.size = new Vector3(1, 1, 1);
                    field.SetValue(o, data);
                }
            }

            _selectedBox = null;

            //Debug.Log("Init");

        }

        void UpdateObjectValue()
        {
            if (isArray)
            {
                field.SetValue(owner, dataArray);
            }
            else
            {
                field.SetValue(owner, data);
            }
        }


        Rect MergeRect(Rect a, Rect b)
        {
            return Rect.MinMaxRect(Mathf.Min(a.xMin, b.xMin), Mathf.Min(a.yMin, b.yMin), Mathf.Max(a.xMax, b.xMax), Mathf.Max(a.yMax, b.yMax));
        }

        public override Rect GetBoundaryRect()
        {
            if (isArray)
            {
                Rect z = Rect.zero;
                foreach (var d in dataArray)
                {
                    z = MergeRect(new Rect(d.center, d.size), z);
                }
                return z;
            }
            else
            {
                return new Rect(data.center, data.size);
            }
        }

        public override void Draw(Event e)
        {
            if (isArray)
            {
                for (int i = 0; i < dataArray.Length; i++)
                {
                    Handles.color = Color.green;

                    Handles.DrawWireCube(dataArray[i].center, dataArray[i].size);
                }


            }
            else
            {
                Handles.color = Color.green;

                Handles.DrawWireCube(data.center, data.size);
            }

            if (_selectedBox != null)
            {
                if (Event.current != null && Event.current.shift)
                {

                    Vector3 size = _selectedBox.size;
                    Vector3 pos = _selectedBox.center;
                    _selectedBox.size = Handles.ScaleHandle(size, pos, Quaternion.identity, HandleUtility.GetHandleSize(pos));

                    if (size != _selectedBox.size)
                    {
                        UpdateObjectValue();
                    }

                    Handles.Label(pos, string.Format("w:{0} h:{1}", size.x, size.z));
                }
                else
                {


                    Vector3 pos = Handles.PositionHandle(_selectedBox.center, Quaternion.identity);

                    if (pos != _selectedBox.center)
                    {
                        _selectedBox.center = pos;
                        UpdateObjectValue();
                    }


                    Handles.Label(pos, string.Format("x:{0} y:{1} z:{2}", pos.x, pos.y, pos.z));
                }

                Handles.color = Color.yellow;
                Handles.DrawWireCube(_selectedBox.center, _selectedBox.size);
            }

        }

        void InvalideScene()
        {
            SceneView.RepaintAll();
        }

        bool HitBox(Box box, Ray ray)
        {
            Bounds bounds = new Bounds(box.center, box.size);
            return bounds.IntersectRay(ray);
        }


        public override void OnMouseDown(Event e)
        {
            //Debug.Log("OnMouseDown");

            //查找BOX
            Ray r = HandleUtility.GUIPointToWorldRay(e.mousePosition);
            //pt.z = 0;

            if (e.control && isArray)
            {
                //创建BOX
                if (e.button == 0)
                {
                    float size = SceneView.lastActiveSceneView.camera.orthographicSize / 4.0f;
                    _selectedBox = new Box();

                    Plane plane = new Plane(Vector3.up, 0);
                    float enter;
                    if (plane.Raycast(r, out enter))
                    {
                        _selectedBox.center = r.GetPoint(enter);
                    }
                    else
                    {
                        _selectedBox.center = r.GetPoint(10);
                    }

                    _selectedBox.size = new Vector3(size, size, size);

                    List<Box> boxList = new List<Box>();
                    boxList.AddRange(dataArray);
                    boxList.Add(_selectedBox);
                    dataArray = boxList.ToArray();
                    field.SetValue(owner, dataArray);

                    SkillEditorWindow.MarkSceneDirty();
                    InvalideScene();
                }
                else if (e.button == 1)
                {
                    foreach (var box in dataArray)
                    {
                        if (HitBox(box, r))
                        {
                            List<Box> boxList = new List<Box>();
                            boxList.AddRange(dataArray);

                            boxList.Remove(box);
                            if (box == _selectedBox)
                            {
                                _selectedBox = null;
                            }

                            dataArray = boxList.ToArray();
                            field.SetValue(owner, dataArray);


                            SkillEditorWindow.MarkSceneDirty();
                            InvalideScene();
                            return;
                        }
                    }


                }
            }
            else
            {
                if (isArray)
                {
                    foreach (var box in dataArray)
                    {
                        if (HitBox(box, r))
                        {
                            //左键选择
                            if (e.button == 0)
                            {
                                _selectedBox = box;

                                InvalideScene();
                                return;
                            }
                        }
                    }
                }
                else
                {
                    Box box = data;
                    if (HitBox(box, r))
                    {
                        //左键选择
                        if (e.button == 0)
                        {
                            _selectedBox = box;

                            InvalideScene();
                            return;
                        }
                    }
                }
            }
        }

        public override void OnMouseUp(Event e)
        {
            //Debug.Log("OnMouseUp");
        }
    }


    /// <summary>
    /// 球体编辑器，对应字段sphere，支持数组
    /// </summary>
    class SphereShapeEditor : ShapeEditor
    {
        bool isArray { get { return field.FieldType.IsArray; } }

        Sphere data;

        Sphere[] dataArray;
        Sphere _selectedBox;

        public SphereShapeEditor()
        {
            //Debug.Log("New BoxShapeEditor");
        }

        public override void Init(GameObject gameObject, object o, FieldInfo field)
        {
            base.Init(gameObject, o, field);
            if (isArray)
            {
                dataArray = (Sphere[])field.GetValue(o);
                if (dataArray == null)
                {
                    dataArray = new Sphere[0];
                    field.SetValue(o, dataArray);
                }
            }
            else
            {
                data = (Sphere)field.GetValue(o);
                if (data == null)
                {
                    data = new Sphere();
                    data.radius = 1;
                    field.SetValue(o, data);
                }
            }

            _selectedBox = null;

            //Debug.Log("Init");

        }

        void UpdateObjectValue()
        {
            if (isArray)
            {
                field.SetValue(owner, dataArray);
            }
            else
            {
                field.SetValue(owner, data);
            }
        }


        Rect MergeRect(Rect a, Rect b)
        {
            return Rect.MinMaxRect(Mathf.Min(a.xMin, b.xMin), Mathf.Min(a.yMin, b.yMin), Mathf.Max(a.xMax, b.xMax), Mathf.Max(a.yMax, b.yMax));
        }

        public override Rect GetBoundaryRect()
        {
            if (isArray)
            {
                Rect z = Rect.zero;
                foreach (var d in dataArray)
                {
                    z = MergeRect(new Rect(d.center, new Vector2(d.radius, d.radius)), z);
                }
                return z;
            }
            else
            {
                return new Rect(data.center, new Vector2(data.radius, data.radius));
            }
        }

        void DrawShpere(Sphere sphere)
        {
            //Handles.SphereHandleCap(0, sphere.center, Quaternion.identity, sphere.radius, EventType.Repaint);

            Handles.DrawWireDisc(sphere.center, Vector3.up, sphere.radius);
            Handles.DrawWireDisc(sphere.center, Vector3.left, sphere.radius);
            Handles.DrawWireDisc(sphere.center, Vector3.forward, sphere.radius);
        }

        public override void Draw(Event e)
        {
            if (isArray)
            {
                for (int i = 0; i < dataArray.Length; i++)
                {
                    Handles.color = Color.green;

                    DrawShpere(dataArray[i]);
                }


            }
            else
            {
                Handles.color = Color.green;

                DrawShpere(data);
            }

            if (_selectedBox != null)
            {
                if (Event.current != null && Event.current.shift)
                {
                    Vector3 pos = _selectedBox.center;

                    Vector3 size = new Vector3(_selectedBox.radius, 1, _selectedBox.radius);

                    _selectedBox.radius = Handles.ScaleHandle(size, pos, Quaternion.identity, HandleUtility.GetHandleSize(pos)).x;

                    if (size.x != _selectedBox.radius)
                    {
                        UpdateObjectValue();
                    }

                    Handles.Label(pos, string.Format("w:{0} h:{1}", size.x, size.z));
                }
                else
                {


                    Vector3 pos = Handles.PositionHandle(_selectedBox.center, Quaternion.identity);

                    if (pos != _selectedBox.center)
                    {
                        _selectedBox.center = pos;
                        UpdateObjectValue();
                    }


                    Handles.Label(pos, string.Format("x:{0} y:{1} z:{2}", pos.x, pos.y, pos.z));
                }

                Handles.color = Color.yellow;
                DrawShpere(_selectedBox);
            }

        }

        void InvalideScene()
        {
            SceneView.RepaintAll();
        }

        bool HitSphere(Sphere sphere, Ray ray)
        {
            Vector3 pj = Vector3.Project(sphere.center - ray.origin, ray.direction);
            if (Vector3.Distance(sphere.center, (ray.origin + pj)) < sphere.radius)
            {
                return true;
            }

            return false;
        }

        public override void OnMouseDown(Event e)
        {
            //Debug.Log("OnMouseDown");

            //查找BOX
            Ray r = HandleUtility.GUIPointToWorldRay(e.mousePosition);
            //pt.z = 0;

            if (e.control && isArray)
            {
                //创建BOX
                if (e.button == 0)
                {
                    float size = SceneView.lastActiveSceneView.camera.orthographicSize / 4.0f;
                    _selectedBox = new Sphere();

                    Plane plane = new Plane(Vector3.up, 0);
                    float enter;
                    if (plane.Raycast(r, out enter))
                    {
                        _selectedBox.center = r.GetPoint(enter);
                    }
                    else
                    {
                        _selectedBox.center = r.GetPoint(10);
                    }

                    _selectedBox.radius = size;

                    List<Sphere> boxList = new List<Sphere>();
                    boxList.AddRange(dataArray);
                    boxList.Add(_selectedBox);
                    dataArray = boxList.ToArray();
                    field.SetValue(owner, dataArray);

                    SkillEditorWindow.MarkSceneDirty();
                    InvalideScene();
                }
                else if (e.button == 1)
                {
                    foreach (var box in dataArray)
                    {
                        if (HitSphere(box, r))
                        {
                            List<Sphere> boxList = new List<Sphere>();
                            boxList.AddRange(dataArray);

                            boxList.Remove(box);
                            if (box == _selectedBox)
                            {
                                _selectedBox = null;
                            }

                            dataArray = boxList.ToArray();
                            field.SetValue(owner, dataArray);


                            SkillEditorWindow.MarkSceneDirty();
                            InvalideScene();
                            return;
                        }
                    }


                }
            }
            else
            {
                if (isArray)
                {
                    foreach (var box in dataArray)
                    {
                        if (HitSphere(box, r))
                        {
                            //左键选择
                            if (e.button == 0)
                            {
                                _selectedBox = box;

                                InvalideScene();
                                return;
                            }
                        }
                    }
                }
                else
                {
                    Sphere box = data;
                    if (HitSphere(box, r))
                    {
                        //左键选择
                        if (e.button == 0)
                        {
                            _selectedBox = box;

                            InvalideScene();
                            return;
                        }
                    }
                }
            }
        }

        public override void OnMouseUp(Event e)
        {
            //Debug.Log("OnMouseUp");
        }
    }
}